#include "udp_client.hpp"
#include "udp_err.hpp"
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <memory>
#include <string>


int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        std::cout << "使用格式错误！" << std::endl;
        std::cout << "正确格式：./udp_client IP Port" << std::endl;
        exit(SOCK_ERR);

    }

    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    
    struct sockaddr_in server;
    memset(&server, 0 , sizeof(server));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(argv[1]);
    server.sin_port = htons(atoi(argv[2]));

    char buff[1024];
    while(true)
    {
        //发送消息
        char message[1024];
        std::cout << "请输入信息：";
        fgets(message, sizeof(message), stdin);

        sendto(sock, message, sizeof(message), 0, (struct sockaddr*)&server, sizeof(server));

        //收消息
        struct sockaddr_in temp;
        socklen_t len = sizeof(temp);
        recvfrom(sock, buff, sizeof(buff)-1, 0, (struct sockaddr*)&temp, &len);
        std::cout << "[小冰服务器]# " << message;
        std::cout << buff;
        std::cout << std::endl;
        memset(buff, 0, sizeof(buff));

    }
    // std::cout << "hello client! " << std::endl;
    return 0;
}